package com.ldj.struts.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemSession;
import com.ldj.hibernate.AnnualVeri;
import com.ldj.hibernate.AnnualVeriDAO;
import com.ldj.hibernate.AnnualVeriRepEndDate;
import com.ldj.hibernate.AnnualVeriRepEndDateDAO;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.UnitInfoDAO;
import com.ldj.hibernate.UnitUserDAO;
import com.ldj.hibernate.UnitUser;
import com.ldj.struts.form.AnnualVeriForm;

public class UnitAnnualVeriInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UnitUserDAO unitInfoDao = new UnitUserDAO(request, response);
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		
		if(uid.getUnitUser() != null){
			String unitId = uid.getUnitUser().getEmpInfoId();
			String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
			int y = Integer.parseInt(year); 
			int startYear = y-10;
			int endYear = y+2;
			AnnualVeriForm aForm = (AnnualVeriForm)form;
			AnnualVeriDAO dao = new AnnualVeriDAO(request, response);
			List list = dao.findAllPage(aForm, unitId);
			AnnualVeriRepEndDateDAO annuDao = new AnnualVeriRepEndDateDAO(request, response);
			AnnualVeriRepEndDate annuDate = annuDao.find();
			request.setAttribute("startYear", startYear);
			request.setAttribute("endYear", endYear);
			request.setAttribute("date", annuDate);
			request.setAttribute("list", list);
			return mapping.findForward("list");
		}
		return mapping.findForward("alert");
	}
	
	//单位申请年审书面申请审查详细查询
	public ActionForward unitFileDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UnitInfoDAO unitInfoDao = new UnitInfoDAO(request, response);
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		String unitId = uid.getUnitUser().getEmpInfoId();
		EmployerInfo info = unitInfoDao.findById(unitId);
		request.setAttribute("uInfo", info);
		//AnnualVeriDAO annuVeridao = new AnnualVeriDAO(request, response);
		//年审书面审查表
		//request.setAttribute("annuVeri", annuVeridao.findAnnuVeriByUnitId(unitId));
		return mapping.findForward("detail");
	}
	
  //年审书面审查表下载
	public ActionForward docUnitFileDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UnitInfoDAO unitInfoDao = new UnitInfoDAO(request, response);
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		String unitId = uid.getUnitUser().getEmpInfoId();
		EmployerInfo info = unitInfoDao.findById(unitId);
		request.setAttribute("uInfo", info);
		return mapping.findForward("doc");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		AnnualVeriRepEndDateDAO annuEndDao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuDate = annuEndDao.find();
		Date a = new Date();
		Date b = annuDate.getAnnualVeriRepEndDate();
		//获取年审申报截止日期表中的年度与年审表中的年度字段，进行判断是否可以申请年审
		AnnualVeriRepEndDateDAO dao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuEndDate = dao.find();
       //		获取单位id
		UnitUserDAO unitInfoDao = new UnitUserDAO(request, response);
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		String unitId = uid.getUnitUser().getEmpInfoId();
		String year = annuEndDate.getAnnualVeriRepEndDateYear();

		AnnualVeriDAO annuVeridao = new AnnualVeriDAO(request, response);
		List list = annuVeridao.findByYear(year, unitId);
		if(a.before(b) && (list.size() == 0 || list == null)) {
			return  mapping.findForward("add");
			
		}else{
			String flag = request.getParameter("flag");
			return new ActionForward("/unitAnnualVeri.do?method=detail&flag="+flag, true);
		}
	}
	//年审信息提交
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			//通过年审结束日期判断是否可以申请年审
		AnnualVeriRepEndDateDAO annuEndDao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuDate = annuEndDao.find();
		Date a = new Date();
		Date b = annuDate.getAnnualVeriRepEndDate();
		//获取年审申报截止日期表中的年度与年审表中的年度字段，进行判断是否可以申请年审
		AnnualVeriRepEndDateDAO dao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuEndDate = dao.find();
       //		获取单位id
		UnitUserDAO unitInfoDao = new UnitUserDAO(request, response);
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		String unitId = uid.getUnitUser().getEmpInfoId();
		String year = annuEndDate.getAnnualVeriRepEndDateYear();

		AnnualVeriDAO annuVeridao = new AnnualVeriDAO(request, response);
		List list = annuVeridao.findByYear(year, unitId);
			if(a.before(b) && (list.size() == 0 || list == null)) {
				AnnualVeri av = new AnnualVeri();
				AnnualVeriForm avForm = (AnnualVeriForm)form;
				EmployerInfo eInfo = new EmployerInfo();
				eInfo.setEmpInfoId(unitId);
				av.setEmployerInfo(eInfo);
				av.setAnnualVeriYear(year);
				av.setAnnualVeriReportTime(new Date());
				av.setAnnualVeriState("4");
				av.setAnnualVeriDelOper("0");
				av.setAnnualVeriIsCase("0");
				av.setAnnualVeriOfficeOper(avForm.getAnnualVeriOfficeOper());
				av.setAnnualVeriOfficeOperTel(avForm.getAnnualVeriOfficeOperTel());
				av.setAnnualVeriBookCheckNum(avForm.getAnnualVeriBookCheckNum());
				av.setAnnualVeriIfBreakLaw(avForm.getAnnualVeriIfBookBreakLaw());
				av.setAnnualVeriSynthesiLev(avForm.getAnnualVeriSynthesiLev());
				av.setAnnualVeriIfGh(avForm.getAnnualVeriIfGh());
				av.setAnnualVeriSocSecuRegnum(avForm.getAnnualVeriSocSecuRegnum());
				av.setAnnualVeriIfWorkTime(avForm.getAnnualVeriIfWorkTime());
				av.setAnnualVeriIfHoliday(avForm.getAnnualVeriIfHoliday());
				av.setAnnualVeriIfLaw(avForm.getAnnualVeriIfLaw());
				av.setAnnualVeriFileNum(avForm.getAnnualVeriFileNum());
				av.setAnnualVeriIfSpeciWorker(avForm.getAnnualVeriIfSpeciWorker());
				av.setAnnualVeriIfRigi(avForm.getAnnualVeriIfRigi());
				av.setAnnualVeriIfLowestSalary(avForm.getAnnualVeriIfLowestSalary());
				av.setAnnualVeriIfDelaySalary(avForm.getAnnualVeriIfDelaySalary());
				av.setAnnualVeriIfHaveChildWork(avForm.getAnnualVeriIfHaveChildWork());
				av.setAnnualVeriIfHaveGirlProt(avForm.getAnnualVeriIfHaveGirlProt());
				av.setAnnualVeriIfHaveInsureRig(avForm.getAnnualVeriIfHaveInsureRig());
				av.setAnnualVeriIfPayInsure(avForm.getAnnualVeriIfPayInsure());
				av.setAnnualVeriHaveWorksendNum(avForm.getAnnualVeriHaveWorksendNum());
				av.setAnnualVeriIfBookBreakLaw(avForm.getAnnualVeriIfBookBreakLaw());
				av.setAnnualVeriIfBookDone(avForm.getAnnualVeriIfBookDone());
				av.setAnnualVeriIfPunish(avForm.getAnnualVeriIfPunish());
				av.setAnnualVeriBookResult(avForm.getAnnualVeriBookResult());
				av.setAnnualVeriBookTime(avForm.getDateAnnualVeriBookTime());
				annuVeridao.save(av);
				return new ActionForward("/unitAnnualVeri.do?method=list", true);
			}else{
				return new ActionForward("/unitAnnualVeri.do?method=detail", true);
			}
	}
	
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		AnnualVeriRepEndDateDAO annuEndDao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuDate = annuEndDao.find();
		Date a = new Date();
		Date b = annuDate.getAnnualVeriRepEndDate();
		//获取年审申报截止日期表中的年度与年审表中的年度字段，进行判断是否可以申请年审
		AnnualVeriRepEndDateDAO dao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuEndDate = dao.find();
       //		获取单位id
		UnitUserDAO unitInfoDao = new UnitUserDAO(request, response);
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		String unitId = uid.getUnitUser().getEmpInfoId();
		String year = annuEndDate.getAnnualVeriRepEndDateYear();

		AnnualVeriDAO annuVeridao = new AnnualVeriDAO(request, response);
		List list = annuVeridao.findByYear(year, unitId);
		if(a.before(b) && list != null) {
			AnnualVeri av = (AnnualVeri) list.get(0);
			request.setAttribute("av", av);
			return  mapping.findForward("update");
		}else{
			return mapping.findForward("SecondOrPass");
		}
	}
	
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//获取年审申报截止日期表中的年度与年审表中的年度字段，进行判断是否可以申请年审
		AnnualVeriRepEndDateDAO dao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuEndDate = dao.find();
       //获取单位id
		UnitUserDAO unitInfoDao = new UnitUserDAO(request, response);
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		String unitId = uid.getUnitUser().getEmpInfoId();
		String year = annuEndDate.getAnnualVeriRepEndDateYear();
		AnnualVeriDAO annuVeridao = new AnnualVeriDAO(request, response);
		List list = annuVeridao.findByYear(year, unitId);
		AnnualVeri av = (AnnualVeri) list.get(0);
		request.setAttribute("av", av);
		return mapping.findForward("addDetail");
	
	}
	
	
	
	//年审信息提交
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			//通过年审结束日期判断是否可以申请年审
				AnnualVeriDAO annuVeridao = new AnnualVeriDAO(request, response);
				
				
				AnnualVeriForm avForm = (AnnualVeriForm)form;
				AnnualVeri av = annuVeridao.findById(avForm.getAnnualVeriId());
				av.setAnnualVeriOfficeOper(avForm.getAnnualVeriOfficeOper());
				av.setAnnualVeriOfficeOperTel(avForm.getAnnualVeriOfficeOperTel());
				av.setAnnualVeriBookCheckNum(avForm.getAnnualVeriBookCheckNum());
				av.setAnnualVeriIfBreakLaw(avForm.getAnnualVeriIfBookBreakLaw());
				av.setAnnualVeriSynthesiLev(avForm.getAnnualVeriSynthesiLev());
				av.setAnnualVeriIfGh(avForm.getAnnualVeriIfGh());
				av.setAnnualVeriSocSecuRegnum(avForm.getAnnualVeriSocSecuRegnum());
				av.setAnnualVeriIfWorkTime(avForm.getAnnualVeriIfWorkTime());
				av.setAnnualVeriIfHoliday(avForm.getAnnualVeriIfHoliday());
				av.setAnnualVeriIfLaw(avForm.getAnnualVeriIfLaw());
				av.setAnnualVeriFileNum(avForm.getAnnualVeriFileNum());
				av.setAnnualVeriIfSpeciWorker(avForm.getAnnualVeriIfSpeciWorker());
				av.setAnnualVeriIfRigi(avForm.getAnnualVeriIfRigi());
				av.setAnnualVeriIfLowestSalary(avForm.getAnnualVeriIfLowestSalary());
				av.setAnnualVeriIfDelaySalary(avForm.getAnnualVeriIfDelaySalary());
				av.setAnnualVeriIfHaveChildWork(avForm.getAnnualVeriIfHaveChildWork());
				av.setAnnualVeriIfHaveGirlProt(avForm.getAnnualVeriIfHaveGirlProt());
				av.setAnnualVeriIfHaveInsureRig(avForm.getAnnualVeriIfHaveInsureRig());
				av.setAnnualVeriIfPayInsure(avForm.getAnnualVeriIfPayInsure());
				av.setAnnualVeriHaveWorksendNum(avForm.getAnnualVeriHaveWorksendNum());
				av.setAnnualVeriIfBookBreakLaw(avForm.getAnnualVeriIfBookBreakLaw());
				av.setAnnualVeriIfBookDone(avForm.getAnnualVeriIfBookDone());
				av.setAnnualVeriIfPunish(avForm.getAnnualVeriIfPunish());
				av.setAnnualVeriBookResult(avForm.getAnnualVeriBookResult());
				av.setAnnualVeriBookTime(avForm.getDateAnnualVeriBookTime());
				annuVeridao.merge(av);
				return new ActionForward("/unitAnnualVeri.do?method=list", true);
	}
	
	
	
	
//	年审信息提交
	public ActionForward apply(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			//通过年审结束日期判断是否可以申请年审
		AnnualVeriRepEndDateDAO annuEndDao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuDate = annuEndDao.find();
		Date a = new Date();
		Date b = annuDate.getAnnualVeriRepEndDate();
		//获取年审申报截止日期表中的年度与年审表中的年度字段，进行判断是否可以申请年审
		AnnualVeriRepEndDateDAO dao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuEndDate = dao.find();
       //		获取单位id
		UnitUserDAO unitInfoDao = new UnitUserDAO(request, response);
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		String unitId = uid.getUnitUser().getEmpInfoId();
		String year = annuEndDate.getAnnualVeriRepEndDateYear();

		AnnualVeriDAO annuVeridao = new AnnualVeriDAO(request, response);
		List list = annuVeridao.findByYear(year, unitId);
		if(a.before(b) && (list.size() == 0 || list == null)) {
			AnnualVeri av = new AnnualVeri();
			av.setAnnualVeriState("0");
			AnnualVeriDAO avDao = new AnnualVeriDAO(request, response);
			avDao.merge(av);
			return new ActionForward("/unitAnnualVeri.do?method=list", true);
		}
		return mapping.findForward("SecondOrPass");
	}
	
	
}